package com.kbbywyclient.ui.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.ImageUpdate;
import com.kbbywyclient.data.entity.WorkCardImages;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.InfoCertifyCreditModule;
import com.kbbywyclient.presenter.InfoCertifyCreditPersenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.InfoCertifyCreditContract;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.util.GalleryFinalUtil;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.BottomPopupOption;
import com.kbbywyclient.widget.CustomToolBar;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * Description: 贷款 资料认证
 * Created by FHW
 * on 2017/5/3
 */

public class InfoCertifyCreditActivity extends BaseActivity implements InfoCertifyCreditContract.View {

    @BindView(R.id.v_toolbar)
    CustomToolBar v_toolbar;

    @BindView(R.id.ll_work_permit)
    LinearLayout ll_work_permit;

    @BindView(R.id.iv_work_permit)
    ImageView iv_work_permit;

    @Inject
    InfoCertifyCreditPersenter infoCertifyCreditPersenter;

    private BottomPopupOption bottomPopupOption;//pop弹窗

    private String salesType = "creditcard";

    private String workCardImages1 = "";

    private int mType = 0;//0-贷款业务 1-信用卡业务
    private boolean isModify = false;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_info_certify_credit;
    }

    @OnClick({R.id.tv_submit, R.id.iv_work_permit})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_submit:
                if (StringUtil.isNotEmpty(workCardImages1)) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("mType", mType);
                    ActivitySkipUtil.skipAnotherActivity(this, InfoCertifySucActivity.class, map);
//                    EventBus.getDefault().post(new Event.SettingUpdate("5"));
                    finish();
                } else {
                    ToastUtil.showShort("请上传工作证");
                }
                break;
            case R.id.iv_work_permit:
                bottomPopupOption.showPopupWindow();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("信用卡-资质认证");
    }

    @Override
    protected void initView() {

        mType = getIntent().getIntExtra("mType", 0);
        isModify = getIntent().getBooleanExtra("isModify", false);

        v_toolbar.setLeftViewOnClickLinster(v -> finish());

        bottomPopupOption = new BottomPopupOption(this);
        bottomPopupOption.setItemText("拍 照", "手机相册");
        bottomPopupOption.setItemClickListener(position -> {
            switch (position) {
                case 0://拍 照
                    GalleryFinalUtil.openCamera(mOnHanlderResultCallback);
                    break;
                case 1://手机相册
                    GalleryFinalUtil.openGallerySingle(mOnHanlderResultCallback);
                    break;
            }
            bottomPopupOption.dismiss();
        });
    }

    private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback = new GalleryFinal.OnHanlderResultCallback() {
        @Override
        public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
            setImaView(resultList);
        }

        @Override
        public void onHanlderFailure(int requestCode, String errorMsg) {

        }
    };

    /**
     * 设置图片
     *
     * @param resultList
     */
    private void setImaView(List<PhotoInfo> resultList) {
        File path = new File(resultList.get(0).getPhotoPath());
        infoCertifyCreditPersenter.getImagesUpload(PreUtils.getUser(this).getSalesID() + "", salesType
                , "WorkCardImages", path);
    }

    @Override
    public void initData() {
        infoCertifyCreditPersenter.getWorkCardImagesCredit(PreUtils.getUser(this).getSalesID() + "");
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getInfoCertifyCreditComponent(new InfoCertifyCreditModule(this)).inject(this);
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadSucWorkCard(WorkCardImages workCardImages) {
        this.workCardImages1 = workCardImages.getWorkCardImages();
        HashMap<String, Object> map = new HashMap<>();
        map.put("mType", mType);

        if (StringUtil.isNotEmpty(workCardImages1)) {
            if (!isModify) {
                ActivitySkipUtil.skipAnotherActivity(this, InfoCertifySucActivity.class, map);
                finish();
            }
        }

        if (StringUtil.isNotEmpty(this.workCardImages1)) {
            Picasso.with(InfoCertifyCreditActivity.this).load(API.PIC_ADDRESS + this.workCardImages1).resize(235, 140).centerCrop().into(iv_work_permit);
        }

    }

    @Override
    public void loadFaiWorkCard(String errorMsg) {

    }

    @Override
    public void loadSucUpdateImg(ImageUpdate imageUpdate) {
        Picasso.with(InfoCertifyCreditActivity.this).load(API.PIC_ADDRESS + imageUpdate.getUrl()).resize(235, 140).centerCrop().into(iv_work_permit);
        workCardImages1 = imageUpdate.getUrl();
    }

    @Override
    public void loadFaiUpdateImg(String errorMsg) {

    }
}
